package com.huanf;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Random;

public class Array2D {

    @Test
    void createArray() {
        int[][] scores = {
                {85,96,85,75},
                {99,96,74,72,75},
                {52,42,56,75}
        };

        System.out.println("一共有" + scores.length +"组成绩.");

        //[[：代表二维数组，I代表元素类型是int
        System.out.println(Arrays.toString(scores[0]));
        //[：代表一维数组，I代表元素类型是int
        System.out.println(Arrays.toString(scores[1]));
        System.out.println(Arrays.toString(scores[2]));

        System.out.println("第1组有" + scores[0].length +"个学员.");
        System.out.println("第2组有" + scores[1].length +"个学员.");
        System.out.println("第3组有" + scores[2].length +"个学员.");

        System.out.println("第1组的每一个学员成绩如下：");
        //第一行的元素
        System.out.println(scores[0][0]);//85
        System.out.println(scores[0][1]);//96
        System.out.println(scores[0][2]);//85
        System.out.println(scores[0][3]);//75
    }

    @Test
    void forEachArray1() {
        int[][] scores = {
                {85,96,85,75},
                {99,96,74,72,75},
                {52,42,56,75}
        };
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                System.out.print(scores[i][j]+" ");
            }
            System.out.println();
        }
    }

    @Test
    void forEachArray2() {
        int[][] scores = {
                {85,96,85,75},
                {99,96,74,72,75},
                {52,42,56,75}
        };

        for (int[] a : scores) {
            for (int one : a) {
                System.out.print(one+" ");
            }
            System.out.println();
        }
    }

    @Test
    void dynamicArray() {
        // 5个学生，每个学生有2门科目的成绩
        int[][] scores = new int[5][2];

        // 随机生成成绩
        Random random = new Random();
        String[] subjects = {"数学", "语文"};
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 2; j++) {
                scores[i][j] = random.nextInt(101); // 随机生成一个0到100的成绩
            }
        }

        // 输出学生的成绩
        for (int i = 0; i < 5; i++) {
            System.out.print("学生" + (i + 1) + "的成绩: ");
            for (int j = 0; j < 2; j++) {
                System.out.print(subjects[j] + ": " + scores[i][j] + " ");
            }
            System.out.println();
        }
    }

}